package zlgl;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import jxl.write.DateTime;
import util.BMUtil;
import xtgl.User;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class WgjyController extends Controller {

    public static int g_infNumber = 10;
    public static String wlbm = null;
    public static String wlmc = null;

    public void index() {
        g_infNumber = 10;
        wlbm = null;
        wlmc = null;
        render("/zlgl/wgjy.html");
    }

    //检验
    public void jy() {                                                      //点击检验弹出填充div
        String ids = getPara("idlist");
        String idlist = "(" + getPara("idlist") + ")";
        setSessionAttr("idlist", ids);
        Page<?> jy = Db.paginate(getParaToInt("pageNumber", 1), g_infNumber,
                "select * ", "from wgjy where id in" + idlist);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("jy", jy);
        List<Object> data = new ArrayList<Object>();
        data.add(map);
        renderJson(data);
    }

    //检验完成
    public void jywc() {
        String idlist = getSessionAttr("idlist");                       //获取选中的ids
        String[] ids = idlist.split(",");
        User user = getSessionAttr("user");                             //获取User ——> 检验人
        for (int i = 0; i < ids.length; i++) {
            String jydh = Db.queryStr("select max(jydh) from wgjy");
            if (jydh == null) {                                             //生成检验单号
                jydh = BMUtil.getBM("WGJY", 001);
            } else {
                jydh = BMUtil.getBM(jydh);
            }
            int id = Integer.parseInt(ids[i]);
            SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Wgjy.dao.findById(id).set("jydh", jydh).set("jysj", fmt.format(new Date())).set("sjsl", getParaToInt("sjsl" + i))
                    .set("cysl", getParaToInt("cysl" + i)).set("hgsl", getParaToInt("hgsl" + i)).set("jyr", user.getStr("name"))
                    .set("bhgsl", getParaToInt("bhgsl" + i)).update();
        }
        renderText("success");
    }

    //新增检验单
    public void addjy() {
        Wgjy wgjy = getModel(Wgjy.class);
        List<Wgjy> wgjys = Wgjy.dao.find("select * from wgjy where id in(select max(id) from wgjy)");
        if (wgjys.size() == 0) {
            wgjy.set("id", 1);
        } else {
            wgjy.set("id", wgjys.get(0).getInt("id") + 1);
        }
        wgjy.save();
        renderNull();
    }

    //...
    public void addMore() {
        for (int i = 0; i < 100; i++) {
            Wgjy.dao.set("id", i + 1).set("xqdh", "170" + i).set("wlbm", "物料编码" + i).set("wlmc", "贴片电阻" + i).set("ggxh", "MAX-" + i)
                    .set("dw", "组").set("bhpc", "" + i).set("bz", "无").save();
        }
        renderText("success");
    }

    //修改检验单
    public void updatejy() {
        Wgjy wgjy = getModel(Wgjy.class, "s");
        wgjy.update();
        renderText("success");
    }

    //待检验
    public void djy() {
        if (getPara("wlbm") != null || getPara("wlmc") != null) {
            wlbm = getPara("wlbm");
            wlmc = getPara("wlmc");
        }
        if (getParaToInt("infNumber") != null) {
            g_infNumber = getParaToInt("infNumber");
        }
        if (wlbm == null || wlmc == null) {
            Page<?> wgjy = Db.paginate(getParaToInt("pageNumber", 1), g_infNumber, "select * ",
                    "from wgjy where ISNULL(jydh)");
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("wgjy", wgjy);
            List<Object> data = new ArrayList<Object>();
            data.add(map);
            renderJson(data);
        } else {
            Page<?> wgjy = Db.paginate(getParaToInt("pageNumber", 1),
                    g_infNumber, "SELECT * ",
                    "FROM wgjy where ISNULL(jydh) and wlbm like '%" + wlbm + "%' and wlmc like '%" + wlmc + "%'");
            setAttr("pN", wgjy.getPageNumber());
            setAttr("tP", wgjy.getTotalPage());
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("wgjy", wgjy);
            List<Object> data = new ArrayList<Object>();
            data.add(map);
            renderJson(data);
        }
    }

    //已检验
    public void yjy() {
        if (getPara("wlbm") != null || getPara("wlmc") != null) {
            wlbm = getPara("wlbm");
            wlmc = getPara("wlmc");
        }
        if (getParaToInt("infNumber") != null) {
            g_infNumber = getParaToInt("infNumber");
        }
        if (wlbm == null || wlmc == null) {
            Page<?> wgjy = Db.paginate(getParaToInt("pageNumber", 1), g_infNumber, "SELECT * ",
                    "FROM wgjy where jydh is not null order by jysj desc");
            Map<String, Object> map1 = new HashMap<String, Object>();
            map1.put("wgjy", wgjy);
            List<Object> data = new ArrayList<Object>();
            data.add(map1);
            renderJson(data);
        } else {

            Page<?> wgjy = Db.paginate(getParaToInt("pageNumber", 1),
                    g_infNumber, "SELECT * ", "FROM wgjy where jydh is not null and wlbm like '%" + wlbm +
                            "%' and wlmc like '%" + wlmc + "%'");
            setAttr("pN", wgjy.getPageNumber());
            setAttr("tP", wgjy.getTotalPage());
            Map<String, Object> map1 = new HashMap<String, Object>();
            map1.put("wgjy", wgjy);
            List<Object> data = new ArrayList<Object>();
            data.add(map1);
            renderJson(data);
        }
    }

    //删除
    public void delete() {
        Wgjy.dao.deleteById(getParaToInt("id"));
        System.out.println(getParaToInt("id"));
        renderNull();
    }

    //分页
    public void fanye() {
        g_infNumber = getParaToInt("infNumber");
        renderNull();
    }

    public void fanye2() {
        g_infNumber = getParaToInt("infNumber2");
        renderNull();
    }

    public void getInfNumber() {
        renderJson(g_infNumber);
    }

}
